package com.ymt.bpm.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * utility for getting properties.
 * @author fuzheng
 *
 */
public class Settings {
	
	static Logger logger = Logger.getLogger(Settings.class.getName());
	/**
	 * 默认配置文件名称.
	 */
	public static final String SETTINGS = "settings";
	
	/**
	 * 指向可用组件模块相对根目录的变量.
	 */
	public static final String MODULE_PATH_BASE = "modules.path.base";
	
	/**
	 * 存放默认配置的map.
	 */
	private static Map<String, Properties> config = null;

	static {
		config = new HashMap<String, Properties>();
	}

	/**
	 * 从默认配置文件获取属性配置.
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		return getValue(SETTINGS, key);
	}
	
	/**
	 * 从某个配置文件获取属性配置.
	 * @param propName
	 * @param key
	 * @return
	 */
	public static String getValue(String propName, String key) {
		try {
			Properties p = config.get(propName);
			if (p==null) {
				load(propName);
				p = config.get(propName);
			}
			return p.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(Settings.class.getName() + " : " + e.toString());
			return null;
		}
	}
	
	/**
	 * 加载指定名称配置文件.
	 * @param propName
	 */
	private static void load(String propName) {
		InputStream in = Settings.class.getClassLoader()
				.getResourceAsStream(propName + ".properties");
		Properties p = new Properties();
		try {
			p.load(in);
			in.close();
			config.put(propName, p);
		} catch (IOException e) {
			logger.finer("No " + propName + ".properties defined error");
		}
	}

}
